import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { momentApi } from '../api'
import type {
  Moment,
  CreateMomentRequest,
  UpdateMomentRequest,
  MomentListRequest,
  MoodStats,
  TagStats
} from '../types'

export function useMoments() {
  const loading = ref(false)
  const moments = ref<Moment[]>([])
  const currentMoment = ref<Moment | null>(null)
  const pagination = reactive({
    page: 1,
    size: 10,
    total: 0
  })

  // 获取动态列表
  const fetchMoments = async (params?: Partial<MomentListRequest>) => {
    loading.value = true
    try {
      const requestParams: MomentListRequest = {
        page: pagination.page,
        size: pagination.size,
        ...params
      }
      const response = await momentApi.list(requestParams)
      moments.value = response.records
      pagination.total = response.total
      pagination.page = response.current
    } catch (error: any) {
      ElMessage.error(error.message || '获取动态列表失败')
    } finally {
      loading.value = false
    }
  }

  // 获取动态详情
  const fetchMomentDetail = async (id: string) => {
    loading.value = true
    try {
      currentMoment.value = await momentApi.detail(id)
    } catch (error: any) {
      ElMessage.error(error.message || '获取动态详情失败')
    } finally {
      loading.value = false
    }
  }

  // 创建动态
  const createMoment = async (data: CreateMomentRequest): Promise<boolean> => {
    loading.value = true
    try {
      const newMoment = await momentApi.create(data)
      moments.value.unshift(newMoment)
      ElMessage.success('创建成功')
      return true
    } catch (error: any) {
      ElMessage.error(error.message || '创建失败')
      return false
    } finally {
      loading.value = false
    }
  }

  // 更新动态
  const updateMoment = async (data: UpdateMomentRequest): Promise<boolean> => {
    loading.value = true
    try {
      const updatedMoment = await momentApi.update(data)
      const index = moments.value.findIndex(m => m.id === data.id)
      if (index !== -1) {
        moments.value[index] = updatedMoment
      }
      currentMoment.value = updatedMoment
      ElMessage.success('更新成功')
      return true
    } catch (error: any) {
      ElMessage.error(error.message || '更新失败')
      return false
    } finally {
      loading.value = false
    }
  }

  // 删除动态
  const deleteMoment = async (id: string): Promise<boolean> => {
    loading.value = true
    try {
      await momentApi.delete(id)
      moments.value = moments.value.filter(m => m.id !== id)
      ElMessage.success('删除成功')
      return true
    } catch (error: any) {
      ElMessage.error(error.message || '删除失败')
      return false
    } finally {
      loading.value = false
    }
  }

  // 分页处理
  const handlePageChange = (page: number) => {
    pagination.page = page
    fetchMoments()
  }

  const handleSizeChange = (size: number) => {
    pagination.size = size
    pagination.page = 1
    fetchMoments()
  }

  return {
    loading,
    moments,
    currentMoment,
    pagination,
    fetchMoments,
    fetchMomentDetail,
    createMoment,
    updateMoment,
    deleteMoment,
    handlePageChange,
    handleSizeChange
  }
}

export function useMomentStats() {
  const loading = ref(false)
  const moodStats = ref<MoodStats[]>([])
  const tagStats = ref<TagStats[]>([])
  const popularTags = ref<string[]>([])

  // 获取心情统计
  const fetchMoodStats = async () => {
    loading.value = true
    try {
      moodStats.value = await momentApi.getMoodStats()
    } catch (error: any) {
      ElMessage.error(error.message || '获取心情统计失败')
    } finally {
      loading.value = false
    }
  }

  // 获取标签统计
  const fetchTagStats = async () => {
    loading.value = true
    try {
      tagStats.value = await momentApi.getTagStats()
    } catch (error: any) {
      ElMessage.error(error.message || '获取标签统计失败')
    } finally {
      loading.value = false
    }
  }

  // 获取热门标签
  const fetchPopularTags = async () => {
    try {
      popularTags.value = await momentApi.getPopularTags()
    } catch (error: any) {
      ElMessage.error(error.message || '获取热门标签失败')
    }
  }

  return {
    loading,
    moodStats,
    tagStats,
    popularTags,
    fetchMoodStats,
    fetchTagStats,
    fetchPopularTags
  }
}